﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Mulberry.Model.Ent;
using OMG.Fill.Mulberry.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Mulberry.Model.UI.Controllers
{
    public class MulCropIncomeController : ApiController
    {
        MulCropIncomeServices mulCropIncomeServices = new MulCropIncomeServices();
        DictServices dict = new DictServices();
        ReportServices reportServices = new ReportServices();

        //[ApiActionAuthorize("获取表9套种作物收益调查情况T_MulCropIncome分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<MulCropIncomeEntity>> PostPageList(MulCropIncomeQuery query)
        {
            return mulCropIncomeServices.GetPageList(query);
        }
        public ResResult<List<MulCropStatsModel>> PostStatsList(MulNowModel model)
        {
            return mulCropIncomeServices.GetmulQuartProducStatsList(model);
        }
        //[ApiActionAuthorize("新增表9套种作物收益调查情况T_MulCropIncome指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(List<MulCropIncomeEntity> model) {
            return mulCropIncomeServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改表9套种作物收益调查情况T_MulCropIncome指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(MulCropIncomeEntity model)
        {
            return mulCropIncomeServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除表9套种作物收益调查情况T_MulCropIncome指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return mulCropIncomeServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取表9套种作物收益调查情况T_MulCropIncome指定对象操作")]
        //[ApiActionException]
        public ResResult<MulCropIncomeEntity> Get(string id)
        {
            return mulCropIncomeServices.GetModel(id);
        }

        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportCode, string FillInYear, string AreaCode)
        {
            AreaServices areaServices = new AreaServices();
            string AreaName = areaServices.GetByCodeModel(AreaCode).model.Name;
            MulNowModel model = new MulNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.ReportCode = ReportCode;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear +AreaName+ "套种作物收益情况调查表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulCropIncomeServices.Method(model).GetBuffer());
            curContext.Response.End();
        }


        public void GetStatsExcelData(string FillInYear, string AreaCode, string Status)
        {
            MulNowModel model = new MulNowModel();
            model.FillInYear = FillInYear;
            model.AreaCode = AreaCode;
            model.Status = Status;
            //string ProvinceCode = "530000000000";
            AreaServices area = new AreaServices();
            List<MulCropStatsModel> list = new List<MulCropStatsModel>();
            if (!string.IsNullOrEmpty(model.AreaCode))
            {
                var listMulArea = mulCropIncomeServices.GetmulQuartProducStatsList(model);
                list = listMulArea.model;
            }
            //string AreaName = area.GetByCodeModel(model.AreaCode).model.Name;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = FillInYear + "全省套种作物收益情况调查表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulCropIncomeServices.MethodStats(list).GetBuffer());
            curContext.Response.End();
        }
    }
}